<template>
  <div>
    <ly-dialog
      v-model="dialogVisible"
      :title="loadingTitle"
      width="560px"
      :before-close="handleClose"
    >
      <el-form
        :inline="false"
        :model="formData"
        :rules="rules"
        ref="rulesForm"
        label-position="right"
        label-width="130px"
      >
        <el-form-item label="计划入校时间:" prop="admission_time">
          <!-- <el-input
              v-model.trim="formData.admission_time"
              placeholder="请输入计划入校时间"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input> -->
          <el-date-picker
            style="width: 300px"
            v-model="formData.admission_time"
            type="date"
            placeholder="选择日期"
          />
          <!-- <el-time-picker v-model="formData.admission_time" placeholder="Arbitrary time" /> -->
        </el-form-item>

        <el-form-item label="备注原因:" prop="reason_remark">
          <el-input
            v-model.trim="formData.reason_remark"
            placeholder="备注原因"
            style="width: 300px"
            :disabled="loadingTitle == '详情'"
          ></el-input>
        </el-form-item>

        <el-form-item label="入校计划审核" prop="state">
          <el-select
            v-model="formData.state"
            placeholder="入校计划审核"
            clearable
            style="width: 300px"
            @change="search(3, $event)"
          >
            <el-option
              v-for="item in inSchoolPlan"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="是否排课" prop="is_status">
            <el-select
              v-model="formData.is_status"
              placeholder="是否排课"
              clearable
              style="width: 300px"
              @change="search(4, $event)">
              <el-option
                v-for="item in isStatusList"
                :key="item.id"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item> -->
      </el-form>
      <template #footer>
        <el-button @click="handleClose" :loading="loadingSave">取消</el-button>
        <el-button
          type="primary"
          @click="submitData"
          :loading="loadingSave"
          :disabled="loadingTitle == '详情'"
          >确定</el-button
        >
      </template>
    </ly-dialog>
  </div>
</template>
  
  <script>
import {
  schoolAdd,
  schoolUpdate,
  addressAreaRoot,
  rstaffschoolAdd,
} from "@/api/api"; 
import LyDialog from "@/components/dialog/dialog.vue";
import { dayjs } from 'element-plus';
export default {
  emits: ["refreshData"],
  name: "addAdmin",
  components: { LyDialog },
  data() {
    return {
      dialogVisible: false,
      loadingSave: false,
      loadingTitle: "",
      userIdList: [],
      planList: [
        {
          value: "1",
          label: "待制定",
          id: "1",
        },
        {
          value: "2",
          label: "已制定",
          id: "2",
        },
      ],
      inSchoolPlan: [
        {
          value: "1",
          label: "待审核",
          id: "1",
        },
        {
          value: "2",
          label: "已审核",
          id: "2",
        },
      ],
      isStatusList: [
        {
          value: "1",
          label: "未排课",
          id: "1",
        },
        {
          value: "2",
          label: "已排课",
          id: "2",
        },
      ],
      formData: {},
      rules: {
        state: [
          { required: true, message: "请选择入校计划审核", trigger: "blur" },
        ],
      },
      GroupList: [], //管理员分组列表
      AreaRootList: [], //区域列表获取
      passwordSwitch: false, //是否修改密码
      linePlanList: [
        {
          value: "1",
          label: "是",
          id: "1",
        },
        {
          value: "2",
          label: "否",
          id: "2",
        },
      ],
    };
  },
  methods: {
    search(i, e) {
      if (i === 1)
        this.formData.user_id = this.userIdList.find(
          (item) => item.value == e
        ).value;
      if (i === 2)
        this.formData.instrument = this.linePlanList.find(
          (item) => item.value == e
        ).value;
      if (i === 3)
        this.formData.state = this.inSchoolPlan.find(
          (item) => item.value == e
        ).value;
      if (i === 4)
        this.formData.is_status = this.isStatusList.find(
          (item) => item.value == e
        ).value;
    },
    getData(e) {
      this.formData.status = e;
    },
    handleClose() {
      this.dialogVisible = false;
      this.loadingSave = false;
      this.formData = {};
      this.passwordSwitch = false;
      this.$emit("refreshData");
    },
    addModuleFn(item, flag, GroupList) {
      this.loadingTitle = flag;
      this.dialogVisible = true;
      if (!this.GroupList&&!this.GroupList.length) this.GroupList = GroupList;
      if (item) {
        this.formData = item;
        this.formData.status = Number(this.formData.status);
      }
    },

    submitDataOne() {
      let vm = this;

      this.$refs["rulesForm"].validate((obj) => {
        if (obj) {
          this.loadingSave = true;

          if (vm.passwordSwitch && vm.formData.passwordEdit) {
            // 有修改密码 修改函数和原始密码函数替换 删除修改的函数
            vm.formData.password = vm.formData.passwordEdit;
            delete vm.formData.passwordEdit;
          }
          let param = {
            ...this.formData,
          };
          Object.assign(param, { ["status_text"]: undefined });

          let objs = {
            id: this.formData.id,
            admission_time: this.formData.admission_time,
            reason_remark: this.formData.reason_remark,
            status: this.formData.state,
          };
          if (this.formData.id) {
            rstaffschoolAdd(objs).then((res) => {
              this.loadingSave = false;
              if (res.code == 1) {
                this.$message.success(res.msg);
                this.handleClose();
                this.$emit("refreshData");
              } else {
                this.$message.warning(res.msg);
              }
            });
          } else {
            console.log(objs, "-------objs");
            rstaffschoolAdd(objs).then((res) => {
              this.loadingSave = false;
              if (res.code == 1) {
                this.$message.success(res.msg);
                this.handleClose();
                this.$emit("refreshData", 1);
              } else {
                this.$message.warning(res.msg);
              }
            });
          }
        }
      });
    },
    submitData() {
      let vm = this;
      vm.submitDataOne();
    },
    imgBeforeUpload(file) {
      const isJPG = file.type === "image/jpeg" || file.type === "image/png";
      if (!isJPG) {
        this.$message.error("图片只能是 JPG/PNG 格式!");
        return false;
      }
      return isJPG;
    },
    // async imgUploadRequest(param) {
    //     var vm = this
    //     let obj= await platformsettingsUploadPlatformImg(param)
    //     if(obj.code == 1) {
    //         let res=''
    //         if (obj.data.data[0].indexOf("://")>=0){
    //             res = obj.data.data[0]

    //         }else{
    //             res = url.split('/api')[0]+obj.data.data[0]
    //         }
    //         vm.formData.default_image = res
    //     } else {
    //         vm.$message.warning(res.msg)
    //     }
    // },
    imgUploadSuccess(res) {
      // if (res) {
      //     this.formData.image = res.url
      // }
    },
    getAreaRoot() {
      // 地区列表获取
      let objs = {
        page: 1,
        per_page: 999, //设置最长数据
      };
      addressAreaRoot(objs).then((res) => {
        if (res.code == 1) {
          console.log(res, "数据获取成功------------");
          this.AreaRootList = res.data.data;
        }
      });
    },
  },
  mounted() {
    console.log("开始获取，‘-----------", this.refreshData);
    //this.getAreaRoot();
  },
};
</script>
  <style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}

.avatar {
  width: 128px;
  height: 128px;
  display: block;
}
</style>
  